package com.hu.config.interceptor;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;

@EnableWebMvc
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
    //将拦截器注入容器
    @Bean
    MyInter myInter(){
        return new MyInter();
    }

    /**
     * 配置拦截器
     */
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(myInter())
                .addPathPatterns("/**")//需要拦截的请求
                .excludePathPatterns("/admin/login")
                .excludePathPatterns("/**/exit");//不拦截的请求

    }
    /**
     * 配置静态访问资源
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        super.addResourceHandlers(registry);
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedMethods("GET", "OPTIONS", "POST", "PUT", "PATCH", "DELETE")
                .allowedOrigins("*")
                .allowedHeaders("Content-Type, Access-Control-Allow-Headers, Authorization, Access-Toke")
                .exposedHeaders("Content-Length", "Link")
                .allowCredentials(true).maxAge(3600);
    }

}
